package com.ljpz.web.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.ljpz.common.service.ApiService;
import com.ljpz.common.service.RedisService;
import com.ljpz.manage.pojo.ItemDesc;
import com.ljpz.manage.pojo.ItemParam;
import com.ljpz.manage.pojo.ItemParamItem;
import com.ljpz.web.bean.Item;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Created by liujunchen on 2016/12/18.
 */
@Service
public class ItemService {
    @Autowired
    private RedisService redisService  ;
    @Autowired
    private ApiService apiService ;
    @Value("${LJPZ_MANAGE_URL}")
    private  String  LJPZ_MANAGE_URL ;
     private  static  final ObjectMapper MAPPER = new ObjectMapper() ;
    public    static  final  String REDIS_KEYS="LJPZ_WEB_ITEM_DETAIL_";
    public  static  final  Integer  REDIS_TIME=60*24;


    /**
     * 根据商品id查询商品数据
     *
     * 通过后台系统提供的接口服务进行查询
     *
     * @param itemId
     * @return
     */
    public Item queryById(Long itemId) {
        String key = REDIS_KEYS+itemId ;
        try {
            String cachData= this.redisService.get(key) ;
            if (StringUtils.isNotEmpty(cachData)) {
                return MAPPER.readValue(cachData, Item.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = LJPZ_MANAGE_URL+"/rest/api/item/"+ itemId;
        try {
            String jsonData= this.apiService.doGet(url )  ;
            if (StringUtils.isEmpty(jsonData)){
                return  null ;
            }
            try {
                this.redisService.set(key,REDIS_TIME,jsonData) ;
            } catch (Exception e) {
                e.printStackTrace();
            }
            //将json数据反序列为Item对象
            return  MAPPER.readValue(jsonData,Item.class ) ;
        } catch (Exception  e) {
            e.printStackTrace();
        }
        return  null ;
    }
    public ItemDesc queryDescById(Long itemId) {
        String   url =  LJPZ_MANAGE_URL+"/rest/api/item/desc/"+itemId ;
        try {
            String jsonData=this.apiService.doGet(url) ;
            if (StringUtils.isEmpty(jsonData))
                return  null ;
            return  MAPPER.readValue(jsonData,ItemDesc.class) ;
        } catch(Exception  e ){
            e.printStackTrace();
        }
        return  null ;
    }
    public String   queryParamByItemId(Long itemId) {
        String url = LJPZ_MANAGE_URL+"/rest/api/item/param/item/"+itemId  ;
        try {
            String jsonData= this.apiService.doGet(url ) ;
            if (StringUtils.isEmpty(jsonData))
                return  null ;
            ItemParamItem  itemParamItem =  MAPPER.readValue(jsonData, ItemParamItem.class);
            String paramData=itemParamItem.getParamData() ;
            //对字符串解析
            ArrayNode arrayNode =   (ArrayNode)MAPPER.readTree(paramData) ;
            //涉及到线程安全的地方用StringBuffer   ，  其他的地方就行StringBuilder
            StringBuilder sb= new StringBuilder() ;
            sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\"><tbody>");
            for (JsonNode param: arrayNode){
                sb.append("<tr><th class=\"tdTitle\" colspan=\"2\">" + param.get("group").asText() + "</th></th>");
                ArrayNode params = (ArrayNode)param.get("params");
                for (JsonNode p : params ){
                    sb.append("<tr><td class=\"tdTitle\">"+p.get("k").asText()+"</td><td>"+p.get("v").asText()+"</td></tr>");
                }
            }
            sb.append("</tbody></table>") ;
            return  sb.toString();

        } catch (Exception e) {
          e.printStackTrace();
        }
        return  null ;
    }
}

